
package io.zhifei.modules.sys.system.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.zhifei.common.annotation.SysLog;
import io.zhifei.common.utils.Constant;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.common.utils.R;
import io.zhifei.common.utils.RedisUtils;
import io.zhifei.common.validator.Assert;
import io.zhifei.common.validator.ValidatorUtils;
import io.zhifei.common.validator.group.AddGroup;
import io.zhifei.modules.sys.system.entity.SysUserEntity;
import io.zhifei.modules.sys.system.form.PasswordForm;
import io.zhifei.modules.sys.system.service.SysUserRoleService;
import io.zhifei.modules.sys.system.service.SysUserService;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p> 用户管理 </p>
 *
 * @author： WangPeng <br/>
 * @date： 2020/9/21 15:51 <br/>
 * @version： <br/>
 */
@RestController
@RequestMapping("/sys/user")
@Api(description = ("后台用户管理"))
public class SysUserController extends AbstractController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private RedisUtils redisUtils;


    /**
     * 所有用户列表
     */
    @GetMapping("/list")
//    @RequiresPermissions("sys:user:list")
    @ApiOperation("用户列表")
    public R list(@RequestParam Map<String, Object> params) {
        //只有超级管理员，才能查看所有管理员列表
        if (getUserId() != Constant.SUPER_ADMIN) {
            params.put("createUserId", getUserId());
        }
        PageUtils pageResult = sysUserService.queryPage(params);
        return R.ok().put("page", pageResult);
    }

    /**
     * 获取登录的用户信息
     */
    @GetMapping("/menuInfo")
    @ApiOperation("获取登录的用户拥有的菜单信息")
    public R info() {
        return R.ok().put("user", sysUserService.getMenuInfo(getUserId()));
    }

    /**
     * 修改登录用户密码
     */
    @SysLog("修改密码")
    @PostMapping("/password")
    @ApiOperation("修改密码")
    public R password(@RequestBody PasswordForm form) {
        Assert.isBlank(form.getNewPassword(), "新密码不为能空");

        //sha256加密
        String password = new Sha256Hash(form.getPassword()).toString();
        //sha256加密
        String newPassword = new Sha256Hash(form.getNewPassword()).toString();

        //更新密码
        boolean flag = sysUserService.updatePassword(form.getUserId(), password, newPassword);
        if (!flag) {
            return R.error("原密码不正确");
        }

        return R.ok();
    }

    /**
     * 用户信息
     */
    @GetMapping("/info/{userId}")
//    @RequiresPermissions("sys:user:info")
    @ApiOperation("用户信息")
    public R info(@PathVariable("userId") Long userId) {
        SysUserEntity user = sysUserService.getById(userId);

        //获取用户所属的角色列表
        List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        user.setRoleIdList(roleIdList);
        return R.ok().put("user", user);
    }

    /**
     * 保存用户
     */
    @SysLog("保存用户")
    @PostMapping("/save")
//    @RequiresPermissions("sys:user:save")
    @ApiOperation("保存用户")
    public R save(@RequestBody SysUserEntity user) {
        SysUserEntity sysUserEntity = getUser();
        ValidatorUtils.validateEntity(user, AddGroup.class);
//        if (user.getCamId() == "" || user.getCamId() ==null){
//            return R.error("校区为必填项！");
//        }
        user.setCreateUserId(getUserId());
        sysUserService.saveUser(user,sysUserEntity);

        return R.ok();
    }

    /**
     * 修改用户
     */
    @SysLog("修改用户")
    @PostMapping("/update")
//    @RequiresPermissions("sys:user:update")
    @ApiOperation("修改用户")
    public R update(@RequestBody SysUserEntity user) {
//        ValidatorUtils.validateEntity(user, UpdateGroup.class);
        SysUserEntity sysUserEntity = getUser();
        user.setCreateUserId(getUserId());
        sysUserService.update(user,sysUserEntity);

        return R.ok();
    }

    /**
     * 删除用户
     */
    @SysLog("删除用户")
    @PostMapping("/delete")
//    @RequiresPermissions("sys:user:delete")
    @ApiOperation("删除用户")
    public R delete(@RequestBody Long[] userIds) {
        if (ArrayUtils.contains(userIds, 1L)) {
            return R.error("系统管理员不能删除");
        }

        if (ArrayUtils.contains(userIds, getUserId())) {
            return R.error("当前用户不能删除");
        }

        sysUserService.deleteBatch(userIds);

        return R.ok();
    }

    /**
     * 所有用户列表
     */
    @GetMapping("/userList")
    @ApiOperation("用户选择列表")
    public R userList() {
        return R.ok().put("page", sysUserService.getUserList());
    }

    @GetMapping(value = "/hello/{name}")
    public String apiHello(@PathVariable String name) {
        return sysUserService.sayHello(name);
    }
}
